<?php
/**
 * Conversations - A lightweight BB for Xaraya
 *
 * @package modules
 * @copyright (C) 2003-2006 The Digital Development Foundation.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.com
 *
 * @subpackage conversations
 * @link http://xaraya.com/index.php/release/760.html
 * @author John Cox
*/
/** 
 * Add new tag / post
 * 
 * @package Xaraya eXtensible Management System
 * @copyright (C) 2005 by the Xaraya Development Team.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.org
 *
*/

/**
 * @author John Cox
 * @author jojodee
 * add new forum topic
 */

function conversations_user_new()
{
    if (!xarVarFetch('phase', 'enum:form:update', $phase, 'form', XARVAR_NOT_REQUIRED, XARVAR_PREP_FOR_DISPLAY)) return;
    if (!xarVarFetch('tag', 'str:1:', $tag, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('post', 'str:1', $post, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('status', 'int:0:2', $status, 1, XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('tagid', 'int:0:', $tagid, '', XARVAR_DONT_SET)) return;
    if (!xarVarFetch('from_post', 'str:1:5', $from_post, '', XARVAR_NOT_REQUIRED)) return;
    
    if (!xarSecurityCheck('Postcon', 1)) return;
    
    $data = array();

    switch(strtolower($phase)) {
        case 'form':
        default:
            $data = array();
        break;

        case 'update':
        
            // Let's begin by setting up some variables
            
            // User Info, we'll use this quite a bit
            $user = xarUserGetVar('uid');
            
            // Let's not to forget to update the user
            // We don't really need an info array at this point, I don't think.  
            // It can be added in updateuser though if necessary.
            // Strike that, i need it later for the subscriptions
            // We will need this info in the loop. 
            $userinfo = xarModAPIFunc('conversations', 'user', 'getusers', array(
                                                    'uid'       => $user));
            //return var_export($userinfo);
            foreach ($userinfo as $micky){
                $oldtagcount = $micky['tagcount'];
                $oldpostcount = $micky['postcount'];
            }
            // TODO
            // Let's look at the karma and maybe give a bonus
            $count = 1;
        
            // First things first, we need to handle the tag.
            // We will split it via a space, ala del.icio.us
            // and then add each one here.
            
            $tags = explode(" ", $tag);
            
            // Do any of the tags exists already?
            $taginfo = array();
            
            foreach ($tags as $new){
                unset($newtagid);
                
                $taginfo = xarModAPIFunc('conversations', 'user', 'gettags', array(
                                                          'tag'   => $new));
                                                          
                //return var_export($taginfo);
                
                // Did the function return false?  OK, then add the tag.
                if (!empty($taginfo)){
                    foreach ($taginfo as $goofy){
                        $newtagid = $goofy['id'];
                    }                 

                } else {

                    $new = strtolower($new);
                    $newtagid = xarModAPIFunc('conversations', 'user', 'createtag', array(
                                                        'tag'   => $new,
                                                        'status'=> $status,
                                                        'uid'   => $user,
                                                        'count' => $count));
                    //return var_export($newtagid);
                
                }
                // Well lookie lookie, we have all of our tag information that we need in taginfo['id']
            
        
            
                // Now we need to create the post
                
                $postinfo = xarModAPIFunc('conversations', 'user', 'createposts', array(
                                                    'text'   => $post,
                                                    'status' => $status,
                                                    'uid'    => $user,
                                                    'count'  => $count));
            

            
                // If this is coming from a post via quick reply, there is no new tag added
                if (!empty($from_post)){
                    $tagcount = 0; 
                } else {                       
                    $tagcount = 1 + $oldtagcount;
                }
            
                // But there is a new post added.
                $postcount = $oldpostcount + 1;
            
                xarModAPIFunc('conversations', 'user', 'updateuser', array(
                                        'uid'       => $user,
                                        'postcount' => $postcount,
                                        'tagcount'  => $tagcount));
            
                // Does the user have the option set for subscribing
                // Automatically?
                
                // $subs = unserialize($userinfo['options']);
                
                // And do something
                
                //if ((isset($subs)) and (isset($subs['subs']))){
                  // set the subscription for these tags
                  
                //}
            
            
                // Let's update our look up table
                // Gotta double check to make sure we aren't coming from a post.

                xarModAPIFunc('conversations', 'user', 'createlook', array(
                                    'tagid'     => $newtagid,
                                    'postid'    => $postinfo)); 

            
                // We need to reset the timestamp for this tag
                // And we might as well give it some bonus points for having a reply
                if(!empty($from_post)){
                    xarModAPIFunc('conversations', 'user', 'updatetag', array(
                                                        'tagid'     => $newtagid,
                                                        'count'     => 20,
                                                        'lastvisit' => time()));
                }
            }
            // Wheeeeeee
            if (!empty($from_post)){
                xarResponseRedirect(xarModURL('conversations', 'user', 'tag', array('tagid' => $newtagid)));
            } else {
                xarResponseRedirect(xarModURL('conversations', 'user', 'main'));
            }
            
        break;
      }

    // Return the output
    return $data;
}

?>